<script setup>
import { ref,reactive,computed,watch,watchEffect,onMounted,nextTick,inject} from "vue";
import { Message } from "../../utils/message";
import DirGroup from "./dir-group.vue";
const props=defineProps({
	title:String,
	children:Array,
    icon:String,
	isOpen:Boolean,
	isGroup:Boolean,
	tabSize:Number,
	handle:Function,
	data:Object
})
const emits=defineEmits(["dir-action"])
const openState=ref(false)
watchEffect(()=>{
	openState.value=props.isOpen
})
function goOn(e){
	emits('dir-action',e)
}
const action=inject("dirActionStatus")
const actionKey=inject("actionKey")
function click(e){
	openState.value=!openState.value
	if(!props.children||props.children.length===0||!props.title){
		emits("dir-action",props.data[actionKey]||props.title)
	}
	if(props.handle){
		props.handle(props.title,props.children,e)
	}
}


const dirIcon=inject("dirIcon",props.dirIcon),
dirIconOpen=inject("dirIconOpen",props.dirIconOpen),
zoomState=inject("zoomState",true);


const isAction=computed(()=>{
	const data=props.data
	if(data[actionKey]){
		return action.value===data[actionKey]
	}else{
		return action.value===props.title
	}
})
</script>

<template>
<div class="dir-item" :class="{isGroup,zoomState:!zoomState}" :style="{'--size':zoomState===false?1:tabSize}">
	<div :class="{'dir-title-action':isAction}"  class="dir-item-title" @click="click">
		<span v-if="icon" class="span"><i class="zijid-ui" :class="icon"></i></span>
		<span v-show="zoomState||!icon" v-html="title" class="dir-item-title-span"></span>
		<div class="dir-state-icon" v-show="zoomState" v-if="children&&children.length">
			<span v-show="openState"><i class="zijid-ui" :class="dirIcon"></i></span>	
			<span v-show="!openState"><i class="zijid-ui" :class="dirIconOpen"></i></span>
		</div>
	</div>
	<template v-if="children&&children.length">
		<dir-group @dir-action="goOn" :data="children" :show="openState" :tabSize="tabSize"></dir-group>
	</template>
</div>

</template>


<style scoped>
.zijid-ui{
	font-size: 1.5em;
}
.dir-item{
	--size:1;
	/* background-color: rgba(255, 255, 255, 0.1); */
}
.dir-item-title{
	cursor: pointer;
	padding-left: calc( var(--size) * 1em );
	padding-top: 1em;
	padding-bottom: 1em;
	height: 1em;
	display: flex;
    justify-content: space-between;
    padding-right: 1em;
    flex-direction: row;
	
    align-items: center;

	flex-wrap: nowrap;
}

.zoomState .dir-item-title{
	justify-content: center;
    align-items: center;
}
.zoomState .dir-item-title-span{
    overflow: hidden !important;
	padding: 0 !important;
}
.zoomState .span{
	padding-right: 0;
}
@media (min-width:600px) {
.dir-item-title:hover {
	background-color: rgba(0, 102, 255, 0.5);
}
}
.dir-item-title:active {
	background-color: rgba(0, 102, 255, 0.5);
}
.isGroup{
	border-bottom: 1px solid rgb(71,71,71);
}

.isGroup.zoomState>.dir-item-title{
	background-color: red;
}

/* .dir-group>.dir-item.zoomState>.dir-item-title{
	background-color: red;
} */
.dir-title-action{
	background-color: rgba(0, 102, 255, 0.8);
}
.span{
	padding-right: 1em;
}
.dir-item-title-span{
	flex:1;
	white-space: nowrap;
	text-overflow: ellipsis;
    overflow: hidden;
}
</style>
